﻿using System;

namespace Business.Entities
{
    [Serializable]
    /// <summary>
    /// Represents a HTTP cookie
    /// </summary>
    public class Cookie
    {
        public string Name { get; set; }
        public string Value { get; set; }
        public string Domain { get; set; }
        public string Path { get; set; }

        public Cookie(string name, string value)
        {
            Name = name;
            Value = value;
        }

        public Cookie(string name, string value, string domain, string path)
        {
            Name = name;
            Value = value;
            Domain = domain;
            Path = path;
        }

        public override string ToString()
        {
            return Name + "=" + Value + " [" + Domain + " | " + Path+"]";
        }
        public Cookie()
        {
        }

        /// <summary>
        /// Checks if the specified url matches the domain/path criteria
        /// </summary>
        /// <param name="url">URL to check for</param>
        /// <returns></returns>
        public bool AppliesFor(string url)
        {
            try
            {
                Uri u = new Uri(url);
                if (!String.IsNullOrEmpty(Domain))
                {
                    if (!u.Host.EndsWith(Domain, StringComparison.OrdinalIgnoreCase)) return false;
                }
                if (!String.IsNullOrEmpty(Path))
                {
                    if (!u.PathAndQuery.StartsWith(Path,StringComparison.OrdinalIgnoreCase)) return false;
                }
                return true;
            }
            catch (Exception)
            {
                return true; // failover
            }
        }
    }
}
